import processTableTpl from './processTable.html';
import processTableCtrl from './processTable.controller';

function routeConfig($stateProvider) {
  'ngInject';

  $stateProvider
    .state('processTable', {
      parent: 'index',
      url: '/securityTable/processTable/:id/:name',
      templateUrl: processTableTpl,
      controller: processTableCtrl,
      controllerAs: 'vm',
      resolve: {
        box($rootScope, $stateParams, documentService) {
          'ngInject';

          const boxName = $stateParams.name;
          return documentService
            .getBoxes($rootScope.orgId)
            .then((res) => {
              const boxes = res.rows;
              const supBox = boxes.filter(box => box.name === boxName)[0];
              supBox.children = boxes.filter(box => box.parentId === supBox.id);
              return supBox;
            });
        },
      },
    });

  $stateProvider
    .state('processTableTodo', {
      parent: 'index',
      url: '/securityTable/processTableTodo/:piId/:todoId/:tableId/:name',
      templateUrl: processTableTpl,
      controller: processTableCtrl,
      controllerAs: 'vm',
      resolve: {
        box($rootScope, $stateParams, documentService) {
          'ngInject';

          const boxName = $stateParams.name;
          return documentService
            .getBoxes($rootScope.orgId)
            .then((res) => {
              const boxes = res.rows;
              const supBox = boxes.filter(box => box.name === boxName)[0];
              supBox.children = boxes.filter(box => box.parentId === supBox.id);
              return supBox;
            });
        },
      },
    });
}

export default routeConfig;
